// @flow
import React, { Component } from 'react'
import styles from './DictAdd.css'
import { Form, Input, Button, message } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
const FormItem = Form.Item
const formItemLayout = {
  labelCol: {
    xs: { span: 24 },
    sm: { span: 6 }
  },
  wrapperCol: {
    xs: { span: 24 },
    sm: { span: 14 }
  }
}

const tailFormItemLayout = {
  wrapperCol: {
    xs: {
      span: 24,
      offset: 0
    },
    sm: {
      span: 14,
      offset: 6
    }
  }
}
type Props = {
  form: Object,
  history: Object
}

type States = {
  loading: boolean
}
class DictAdd extends Component {
  props: Props
  state: States
  getIndustry: Function
  getDictType: Function
  submit: Function
  goBack: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      industries: [],
      customerTypes: [],
      loading: false
    }
    this.submit = this.submit.bind(this)
    this.goBack = this.goBack.bind(this)
  }

  componentWillMount () {
  }

  submit () {
    const { form } = this.props
    form.validateFields((err, fieldsValue) => {
      if (err) {
        return false
      }
      this.setState({
        loading: true
      })
      const values = {
        ...fieldsValue
      }

      universalFetch(`${__SERVICE_API__}sys/dicts`, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify(values)
      })
        .then(res => res.json())
        .then((json) => {
          if (json.code !== 200000) {
            throw new Error(JSON.stringify({ code: json.code, message: json.message }))
          }
          this.setState({
            loading: false
          })
          message.success('添加成功')
          this.props.history.push('/system/dictSetting')
        })
        .catch((error) => {
          handleFetchError(error)
          console.error(error)
          message.error('添加失败')
          this.setState({
            loading: false
          })
        })
    })
  }

  goBack () {
    this.props.history.push('/system/dictSetting')
  }
  render () {
    const { getFieldDecorator } = this.props.form
    const { loading } = this.state
    return (
      <div className={styles['main']}>
        <Form>
          <FormItem {...formItemLayout} label='显示名称' hasFeedback>
            {
              getFieldDecorator('label', {
                rules: [
                  { required: true, message: '请填写显示名称', whitespace: true }
                ]
              })(
                <Input placeholder='请填写显示名称' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='回填内容' hasFeedback>
            {
              getFieldDecorator('value', {
                rules: [
                  { required: true, message: '请填写回填内容', whitespace: true }
                ]
              })(
                <Input placeholder='请填写回填内容' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='字典类型' hasFeedback>
            {
              getFieldDecorator('type', {
                rules: [
                  { required: true, message: '请填写字典类型', whitespace: true }
                ]
              })(
                <Input placeholder='填写字典类型' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='类型描述' hasFeedback>
            {
              getFieldDecorator('typeCn', {
                rules: [
                  { required: true, message: '请填写类型描述', whitespace: true }
                ]
              })(
                <Input placeholder='填写类型描述' />
              )
            }
          </FormItem>
          <FormItem {...formItemLayout} label='显示顺序' hasFeedback>
            {
              getFieldDecorator('sort', {
                rules: [
                  { required: true, message: '请填写显示顺序', whitespace: true }
                ]
              })(
                <Input placeholder='请填写显示顺序' />
              )
            }
          </FormItem>
          <FormItem {...tailFormItemLayout}>
            <Button type='primary'
              loading={loading}
              onClick={this.submit}
              >
              保存
            </Button>
            <Button
              onClick={this.goBack}
              style={{ marginLeft: '20px' }}
              >
              返回
            </Button>
          </FormItem>
        </Form>
      </div>
    )
  }
}

export default Form.create()(DictAdd)
